home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / subprocess.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  17KB  |  750 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import sys
  5. mswindows = sys.platform == 'win32'
  6. import os
  7. import types
  8. import traceback
  9. import gc
  10.  
  11. class CalledProcessError(Exception):
  12.     
  13.     def __init__(self, returncode, cmd):
  14.         self.returncode = returncode
  15.         self.cmd = cmd
  16.  
  17.     
  18.     def __str__(self):
  19.         return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
  20.  
  21.  
  22. if mswindows:
  23.     import threading
  24.     import msvcrt
  25.     from _subprocess import *
  26.     
  27.     class STARTUPINFO:
  28.         dwFlags = 0
  29.         hStdInput = None
  30.         hStdOutput = None
  31.         hStdError = None
  32.         wShowWindow = 0
  33.  
  34.     
  35.     class pywintypes:
  36.         error = IOError
  37.  
  38. else:
  39.     import select
  40.     import errno
  41.     import fcntl
  42.     import pickle
  43. __all__ = [
  44.     'Popen',
  45.     'PIPE',
  46.     'STDOUT',
  47.     'call',
  48.     'check_call',
  49.     'CalledProcessError']
  50.  
  51. try:
  52.     MAXFD = os.sysconf('SC_OPEN_MAX')
  53. except:
  54.     MAXFD = 256
  55.  
  56.  
  57. try:
  58.     False
  59. except NameError:
  60.     False = 0
  61.     True = 1
  62.  
  63. _active = []
  64.  
  65. def _cleanup():
  66.     for inst in _active[:]:
  67.         if inst._internal_poll(_deadstate = sys.maxint) >= 0:
  68.             
  69.             try:
  70.                 _active.remove(inst)
  71.             except ValueError:
  72.                 pass
  73.             except:
  74.                 None<EXCEPTION MATCH>ValueError
  75.             
  76.  
  77.         None<EXCEPTION MATCH>ValueError
  78.     
  79.  
  80. PIPE = -1
  81. STDOUT = -2
  82.  
  83. def call(*popenargs, **kwargs):
  84.     return Popen(*popenargs, **kwargs).wait()
  85.  
  86.  
  87. def check_call(*popenargs, **kwargs):
  88.     retcode = call(*popenargs, **kwargs)
  89.     cmd = kwargs.get('args')
  90.     if cmd is None:
  91.         cmd = popenargs[0]
  92.     
  93.     if retcode:
  94.         raise CalledProcessError(retcode, cmd)
  95.     
  96.     return retcode
  97.  
  98.  
  99. def list2cmdline(seq):
  100.     result = []
  101.     needquote = False
  102.     for arg in seq:
  103.         bs_buf = []
  104.         if result:
  105.             result.append(' ')
  106.         
  107.         if not ' ' in arg and '\t' in arg:
  108.             pass
  109.         needquote = arg == ''
  110.         if needquote:
  111.             result.append('"')
  112.         
  113.         for c in arg:
  114.             if c == '\\':
  115.                 bs_buf.append(c)
  116.                 continue
  117.             if c == '"':
  118.                 result.append('\\' * len(bs_buf) * 2)
  119.                 bs_buf = []
  120.                 result.append('\\"')
  121.                 continue
  122.             if bs_buf:
  123.                 result.extend(bs_buf)
  124.                 bs_buf = []
  125.             
  126.             result.append(c)
  127.         
  128.         if bs_buf:
  129.             result.extend(bs_buf)
  130.         
  131.         if needquote:
  132.             result.extend(bs_buf)
  133.             result.append('"')
  134.             continue
  135.     
  136.     return ''.join(result)
  137.  
  138.  
  139. class Popen(object):
  140.     
  141.     def __init__(self, args, bufsize = 0, executable = None, stdin = None, stdout = None, stderr = None, preexec_fn = None, close_fds = False, shell = False, cwd = None, env = None, universal_newlines = False, startupinfo = None, creationflags = 0):
  142.         _cleanup()
  143.         self._child_created = False
  144.         if not isinstance(bufsize, (int, long)):
  145.             raise TypeError('bufsize must be an integer')
  146.         
  147.         if mswindows:
  148.             if preexec_fn is not None:
  149.                 raise ValueError('preexec_fn is not supported on Windows platforms')
  150.             
  151.             if close_fds:
  152.                 raise ValueError('close_fds is not supported on Windows platforms')
  153.             
  154.         elif startupinfo is not None:
  155.             raise ValueError('startupinfo is only supported on Windows platforms')
  156.         
  157.         if creationflags != 0:
  158.             raise ValueError('creationflags is only supported on Windows platforms')
  159.         
  160.         self.stdin = None
  161.         self.stdout = None
  162.         self.stderr = None
  163.         self.pid = None
  164.         self.returncode = None
  165.         self.universal_newlines = universal_newlines
  166.         (p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  167.         self._execute_child(args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
  168.         if mswindows:
  169.             if stdin is None and p2cwrite is not None:
  170.                 os.close(p2cwrite)
  171.                 p2cwrite = None
  172.             
  173.             if stdout is None and c2pread is not None:
  174.                 os.close(c2pread)
  175.                 c2pread = None
  176.             
  177.             if stderr is None and errread is not None:
  178.                 os.close(errread)
  179.                 errread = None
  180.             
  181.         
  182.         if p2cwrite:
  183.             self.stdin = os.fdopen(p2cwrite, 'wb', bufsize)
  184.         
  185.         if c2pread:
  186.             if universal_newlines:
  187.                 self.stdout = os.fdopen(c2pread, 'rU', bufsize)
  188.             else:
  189.                 self.stdout = os.fdopen(c2pread, 'rb', bufsize)
  190.         
  191.         if errread:
  192.             if universal_newlines:
  193.                 self.stderr = os.fdopen(errread, 'rU', bufsize)
  194.             else:
  195.                 self.stderr = os.fdopen(errread, 'rb', bufsize)
  196.         
  197.  
  198.     
  199.     def _translate_newlines(self, data):
  200.         data = data.replace('\r\n', '\n')
  201.         data = data.replace('\r', '\n')
  202.         return data
  203.  
  204.     
  205.     def __del__(self, sys = sys):
  206.         if not self._child_created:
  207.             return None
  208.         
  209.         self._internal_poll(_deadstate = sys.maxint)
  210.         if self.returncode is None and _active is not None:
  211.             _active.append(self)
  212.         
  213.  
  214.     
  215.     def communicate(self, input = None):
  216.         if [
  217.             self.stdin,
  218.             self.stdout,
  219.             self.stderr].count(None) >= 2:
  220.             stdout = None
  221.             stderr = None
  222.             if self.stdin:
  223.                 if input:
  224.                     self.stdin.write(input)
  225.                 
  226.                 self.stdin.close()
  227.             elif self.stdout:
  228.                 stdout = self.stdout.read()
  229.                 self.stdout.close()
  230.             elif self.stderr:
  231.                 stderr = self.stderr.read()
  232.                 self.stderr.close()
  233.             
  234.             self.wait()
  235.             return (stdout, stderr)
  236.         
  237.         return self._communicate(input)
  238.  
  239.     
  240.     def poll(self):
  241.         return self._internal_poll()
  242.  
  243.     if mswindows:
  244.         
  245.         def _get_handles(self, stdin, stdout, stderr):
  246.             if stdin is None and stdout is None and stderr is None:
  247.                 return (None, None, None, None, None, None)
  248.             
  249.             (p2cread, p2cwrite) = (None, None)
  250.             (c2pread, c2pwrite) = (None, None)
  251.             (errread, errwrite) = (None, None)
  252.             if stdin is None:
  253.                 p2cread = GetStdHandle(STD_INPUT_HANDLE)
  254.             
  255.             if p2cread is not None:
  256.                 pass
  257.             elif stdin is None or stdin == PIPE:
  258.                 (p2cread, p2cwrite) = CreatePipe(None, 0)
  259.                 p2cwrite = p2cwrite.Detach()
  260.                 p2cwrite = msvcrt.open_osfhandle(p2cwrite, 0)
  261.             elif isinstance(stdin, int):
  262.                 p2cread = msvcrt.get_osfhandle(stdin)
  263.             else:
  264.                 p2cread = msvcrt.get_osfhandle(stdin.fileno())
  265.             p2cread = self._make_inheritable(p2cread)
  266.             if stdout is None:
  267.                 c2pwrite = GetStdHandle(STD_OUTPUT_HANDLE)
  268.             
  269.             if c2pwrite is not None:
  270.                 pass
  271.             elif stdout is None or stdout == PIPE:
  272.                 (c2pread, c2pwrite) = CreatePipe(None, 0)
  273.                 c2pread = c2pread.Detach()
  274.                 c2pread = msvcrt.open_osfhandle(c2pread, 0)
  275.             elif isinstance(stdout, int):
  276.                 c2pwrite = msvcrt.get_osfhandle(stdout)
  277.             else:
  278.                 c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
  279.             c2pwrite = self._make_inheritable(c2pwrite)
  280.             if stderr is None:
  281.                 errwrite = GetStdHandle(STD_ERROR_HANDLE)
  282.             
  283.             if errwrite is not None:
  284.                 pass
  285.             elif stderr is None or stderr == PIPE:
  286.                 (errread, errwrite) = CreatePipe(None, 0)
  287.                 errread = errread.Detach()
  288.                 errread = msvcrt.open_osfhandle(errread, 0)
  289.             elif stderr == STDOUT:
  290.                 errwrite = c2pwrite
  291.             elif isinstance(stderr, int):
  292.                 errwrite = msvcrt.get_osfhandle(stderr)
  293.             else:
  294.                 errwrite = msvcrt.get_osfhandle(stderr.fileno())
  295.             errwrite = self._make_inheritable(errwrite)
  296.             return (p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
  297.  
  298.         
  299.         def _make_inheritable(self, handle):
  300.             return DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), 0, 1, DUPLICATE_SAME_ACCESS)
  301.  
  302.         
  303.         def _find_w9xpopen(self):
  304.             w9xpopen = os.path.join(os.path.dirname(GetModuleFileName(0)), 'w9xpopen.exe')
  305.             if not os.path.exists(w9xpopen):
  306.                 w9xpopen = os.path.join(os.path.dirname(sys.exec_prefix), 'w9xpopen.exe')
  307.                 if not os.path.exists(w9xpopen):
  308.                     raise RuntimeError('Cannot locate w9xpopen.exe, which is needed for Popen to work with your shell or platform.')
  309.                 
  310.             
  311.             return w9xpopen
  312.  
  313.         
  314.         def _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite):
  315.             if not isinstance(args, types.StringTypes):
  316.                 args = list2cmdline(args)
  317.             
  318.             if startupinfo is None:
  319.                 startupinfo = STARTUPINFO()
  320.             
  321.             if None not in (p2cread, c2pwrite, errwrite):
  322.                 startupinfo.dwFlags |= STARTF_USESTDHANDLES
  323.                 startupinfo.hStdInput = p2cread
  324.                 startupinfo.hStdOutput = c2pwrite
  325.                 startupinfo.hStdError = errwrite
  326.             
  327.             if shell:
  328.                 startupinfo.dwFlags |= STARTF_USESHOWWINDOW
  329.                 startupinfo.wShowWindow = SW_HIDE
  330.                 comspec = os.environ.get('COMSPEC', 'cmd.exe')
  331.                 args = comspec + ' /c ' + args
  332.                 if GetVersion() >= 0x80000000L or os.path.basename(comspec).lower() == 'command.com':
  333.                     w9xpopen = self._find_w9xpopen()
  334.                     args = '"%s" %s' % (w9xpopen, args)
  335.                     creationflags |= CREATE_NEW_CONSOLE
  336.                 
  337.             
  338.             
  339.             try:
  340.                 (hp, ht, pid, tid) = CreateProcess(executable, args, None, None, 1, creationflags, env, cwd, startupinfo)
  341.             except pywintypes.error:
  342.                 e = None
  343.                 raise WindowsError(*e.args)
  344.  
  345.             self._child_created = True
  346.             self._handle = hp
  347.             self.pid = pid
  348.             ht.Close()
  349.             if p2cread is not None:
  350.                 p2cread.Close()
  351.             
  352.             if c2pwrite is not None:
  353.                 c2pwrite.Close()
  354.             
  355.             if errwrite is not None:
  356.                 errwrite.Close()
  357.             
  358.  
  359.         
  360.         def _internal_poll(self, _deadstate = None):
  361.             if self.returncode is None:
  362.                 if WaitForSingleObject(self._handle, 0) == WAIT_OBJECT_0:
  363.                     self.returncode = GetExitCodeProcess(self._handle)
  364.                 
  365.             
  366.             return self.returncode
  367.  
  368.         
  369.         def wait(self):
  370.             if self.returncode is None:
  371.                 obj = WaitForSingleObject(self._handle, INFINITE)
  372.                 self.returncode = GetExitCodeProcess(self._handle)
  373.             
  374.             return self.returncode
  375.  
  376.         
  377.         def _readerthread(self, fh, buffer):
  378.             buffer.append(fh.read())
  379.  
  380.         
  381.         def _communicate(self, input):
  382.             stdout = None
  383.             stderr = None
  384.             if self.stdout:
  385.                 stdout = []
  386.                 stdout_thread = threading.Thread(target = self._readerthread, args = (self.stdout, stdout))
  387.                 stdout_thread.setDaemon(True)
  388.                 stdout_thread.start()
  389.             
  390.             if self.stderr:
  391.                 stderr = []
  392.                 stderr_thread = threading.Thread(target = self._readerthread, args = (self.stderr, stderr))
  393.                 stderr_thread.setDaemon(True)
  394.                 stderr_thread.start()
  395.             
  396.             if self.stdin:
  397.                 if input is not None:
  398.                     self.stdin.write(input)
  399.                 
  400.                 self.stdin.close()
  401.             
  402.             if self.stdout:
  403.                 stdout_thread.join()
  404.             
  405.             if self.stderr:
  406.                 stderr_thread.join()
  407.             
  408.             if stdout is not None:
  409.                 stdout = stdout[0]
  410.             
  411.             if stderr is not None:
  412.                 stderr = stderr[0]
  413.             
  414.             if self.universal_newlines and hasattr(file, 'newlines'):
  415.                 if stdout:
  416.                     stdout = self._translate_newlines(stdout)
  417.                 
  418.                 if stderr:
  419.                     stderr = self._translate_newlines(stderr)
  420.                 
  421.             
  422.             self.wait()
  423.             return (stdout, stderr)
  424.  
  425.     else:
  426.         
  427.         def _get_handles(self, stdin, stdout, stderr):
  428.             (p2cread, p2cwrite) = (None, None)
  429.             (c2pread, c2pwrite) = (None, None)
  430.             (errread, errwrite) = (None, None)
  431.             if stdin is None:
  432.                 pass
  433.             elif stdin == PIPE:
  434.                 (p2cread, p2cwrite) = os.pipe()
  435.             elif isinstance(stdin, int):
  436.                 p2cread = stdin
  437.             else:
  438.                 p2cread = stdin.fileno()
  439.             if stdout is None:
  440.                 pass
  441.             elif stdout == PIPE:
  442.                 (c2pread, c2pwrite) = os.pipe()
  443.             elif isinstance(stdout, int):
  444.                 c2pwrite = stdout
  445.             else:
  446.                 c2pwrite = stdout.fileno()
  447.             if stderr is None:
  448.                 pass
  449.             elif stderr == PIPE:
  450.                 (errread, errwrite) = os.pipe()
  451.             elif stderr == STDOUT:
  452.                 errwrite = c2pwrite
  453.             elif isinstance(stderr, int):
  454.                 errwrite = stderr
  455.             else:
  456.                 errwrite = stderr.fileno()
  457.             return (p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
  458.  
  459.         
  460.         def _set_cloexec_flag(self, fd):
  461.             
  462.             try:
  463.                 cloexec_flag = fcntl.FD_CLOEXEC
  464.             except AttributeError:
  465.                 cloexec_flag = 1
  466.  
  467.             old = fcntl.fcntl(fd, fcntl.F_GETFD)
  468.             fcntl.fcntl(fd, fcntl.F_SETFD, old | cloexec_flag)
  469.  
  470.         
  471.         def _close_fds(self, but):
  472.             for i in xrange(3, MAXFD):
  473.                 if i == but:
  474.                     continue
  475.                 
  476.                 
  477.                 try:
  478.                     os.close(i)
  479.                 continue
  480.                 continue
  481.  
  482.             
  483.  
  484.         
  485.         def _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite):
  486.             if isinstance(args, types.StringTypes):
  487.                 args = [
  488.                     args]
  489.             else:
  490.                 args = list(args)
  491.             if shell:
  492.                 args = [
  493.                     '/bin/sh',
  494.                     '-c'] + args
  495.             
  496.             if executable is None:
  497.                 executable = args[0]
  498.             
  499.             (errpipe_read, errpipe_write) = os.pipe()
  500.             self._set_cloexec_flag(errpipe_write)
  501.             gc_was_enabled = gc.isenabled()
  502.             gc.disable()
  503.             
  504.             try:
  505.                 self.pid = os.fork()
  506.             except:
  507.                 if gc_was_enabled:
  508.                     gc.enable()
  509.                 
  510.                 raise 
  511.  
  512.             self._child_created = True
  513.             if self.pid == 0:
  514.                 
  515.                 try:
  516.                     if p2cwrite:
  517.                         os.close(p2cwrite)
  518.                     
  519.                     if c2pread:
  520.                         os.close(c2pread)
  521.                     
  522.                     if errread:
  523.                         os.close(errread)
  524.                     
  525.                     os.close(errpipe_read)
  526.                     if p2cread:
  527.                         os.dup2(p2cread, 0)
  528.                     
  529.                     if c2pwrite:
  530.                         os.dup2(c2pwrite, 1)
  531.                     
  532.                     if errwrite:
  533.                         os.dup2(errwrite, 2)
  534.                     
  535.                     if p2cread and p2cread not in (0,):
  536.                         os.close(p2cread)
  537.                     
  538.                     if c2pwrite and c2pwrite not in (p2cread, 1):
  539.                         os.close(c2pwrite)
  540.                     
  541.                     if errwrite and errwrite not in (p2cread, c2pwrite, 2):
  542.                         os.close(errwrite)
  543.                     
  544.                     if close_fds:
  545.                         self._close_fds(but = errpipe_write)
  546.                     
  547.                     if cwd is not None:
  548.                         os.chdir(cwd)
  549.                     
  550.                     if preexec_fn:
  551.                         apply(preexec_fn)
  552.                     
  553.                     if env is None:
  554.                         os.execvp(executable, args)
  555.                     else:
  556.                         os.execvpe(executable, args, env)
  557.                 except:
  558.                     (exc_type, exc_value, tb) = sys.exc_info()
  559.                     exc_lines = traceback.format_exception(exc_type, exc_value, tb)
  560.                     exc_value.child_traceback = ''.join(exc_lines)
  561.                     os.write(errpipe_write, pickle.dumps(exc_value))
  562.  
  563.                 os._exit(255)
  564.             
  565.             if gc_was_enabled:
  566.                 gc.enable()
  567.             
  568.             os.close(errpipe_write)
  569.             if p2cread and p2cwrite:
  570.                 os.close(p2cread)
  571.             
  572.             if c2pwrite and c2pread:
  573.                 os.close(c2pwrite)
  574.             
  575.             if errwrite and errread:
  576.                 os.close(errwrite)
  577.             
  578.             data = os.read(errpipe_read, 1048576)
  579.             os.close(errpipe_read)
  580.             if data != '':
  581.                 os.waitpid(self.pid, 0)
  582.                 child_exception = pickle.loads(data)
  583.                 raise child_exception
  584.             
  585.  
  586.         
  587.         def _handle_exitstatus(self, sts):
  588.             if os.WIFSIGNALED(sts):
  589.                 self.returncode = -os.WTERMSIG(sts)
  590.             elif os.WIFEXITED(sts):
  591.                 self.returncode = os.WEXITSTATUS(sts)
  592.             else:
  593.                 raise RuntimeError('Unknown child exit status!')
  594.  
  595.         
  596.         def _internal_poll(self, _deadstate = None):
  597.             if self.returncode is None:
  598.                 
  599.                 try:
  600.                     (pid, sts) = os.waitpid(self.pid, os.WNOHANG)
  601.                     if pid == self.pid:
  602.                         self._handle_exitstatus(sts)
  603.                 except os.error:
  604.                     if _deadstate is not None:
  605.                         self.returncode = _deadstate
  606.                     
  607.                 except:
  608.                     _deadstate is not None
  609.                 
  610.  
  611.             None<EXCEPTION MATCH>os.error
  612.             return self.returncode
  613.  
  614.         
  615.         def wait(self):
  616.             if self.returncode is None:
  617.                 (pid, sts) = os.waitpid(self.pid, 0)
  618.                 self._handle_exitstatus(sts)
  619.             
  620.             return self.returncode
  621.  
  622.         
  623.         def _communicate(self, input):
  624.             read_set = []
  625.             write_set = []
  626.             stdout = None
  627.             stderr = None
  628.             if self.stdin:
  629.                 self.stdin.flush()
  630.                 if input:
  631.                     write_set.append(self.stdin)
  632.                 else:
  633.                     self.stdin.close()
  634.             
  635.             if self.stdout:
  636.                 read_set.append(self.stdout)
  637.                 stdout = []
  638.             
  639.             if self.stderr:
  640.                 read_set.append(self.stderr)
  641.                 stderr = []
  642.             
  643.             input_offset = 0
  644.             while read_set or write_set:
  645.                 
  646.                 try:
  647.                     (rlist, wlist, xlist) = select.select(read_set, write_set, [])
  648.                 except select.error:
  649.                     e = None
  650.                     if e.args[0] == errno.EINTR:
  651.                         continue
  652.                     
  653.                     raise 
  654.  
  655.                 if self.stdin in wlist:
  656.                     bytes_written = os.write(self.stdin.fileno(), buffer(input, input_offset, 512))
  657.                     input_offset += bytes_written
  658.                     if input_offset >= len(input):
  659.                         self.stdin.close()
  660.                         write_set.remove(self.stdin)
  661.                     
  662.                 
  663.                 if self.stdout in rlist:
  664.                     data = os.read(self.stdout.fileno(), 1024)
  665.                     if data == '':
  666.                         self.stdout.close()
  667.                         read_set.remove(self.stdout)
  668.                     
  669.                     stdout.append(data)
  670.                 
  671.                 if self.stderr in rlist:
  672.                     data = os.read(self.stderr.fileno(), 1024)
  673.                     if data == '':
  674.                         self.stderr.close()
  675.                         read_set.remove(self.stderr)
  676.                     
  677.                     stderr.append(data)
  678.                     continue
  679.             if stdout is not None:
  680.                 stdout = ''.join(stdout)
  681.             
  682.             if stderr is not None:
  683.                 stderr = ''.join(stderr)
  684.             
  685.             if self.universal_newlines and hasattr(file, 'newlines'):
  686.                 if stdout:
  687.                     stdout = self._translate_newlines(stdout)
  688.                 
  689.                 if stderr:
  690.                     stderr = self._translate_newlines(stderr)
  691.                 
  692.             
  693.             self.wait()
  694.             return (stdout, stderr)
  695.  
  696.  
  697.  
  698. def _demo_posix():
  699.     plist = Popen([
  700.         'ps'], stdout = PIPE).communicate()[0]
  701.     print 'Process list:'
  702.     print plist
  703.     if os.getuid() == 0:
  704.         p = Popen([
  705.             'id'], preexec_fn = (lambda : os.setuid(100)))
  706.         p.wait()
  707.     
  708.     print "Looking for 'hda'..."
  709.     p1 = Popen([
  710.         'dmesg'], stdout = PIPE)
  711.     p2 = Popen([
  712.         'grep',
  713.         'hda'], stdin = p1.stdout, stdout = PIPE)
  714.     print repr(p2.communicate()[0])
  715.     print 
  716.     print 'Trying a weird file...'
  717.     
  718.     try:
  719.         print Popen([
  720.             '/this/path/does/not/exist']).communicate()
  721.     except OSError:
  722.         e = None
  723.         if e.errno == errno.ENOENT:
  724.             print "The file didn't exist.  I thought so..."
  725.             print 'Child traceback:'
  726.             print e.child_traceback
  727.         else:
  728.             print 'Error', e.errno
  729.     except:
  730.         e.errno == errno.ENOENT
  731.  
  732.     print >>sys.stderr, 'Gosh.  No error.'
  733.  
  734.  
  735. def _demo_windows():
  736.     print "Looking for 'PROMPT' in set output..."
  737.     p1 = Popen('set', stdout = PIPE, shell = True)
  738.     p2 = Popen('find "PROMPT"', stdin = p1.stdout, stdout = PIPE)
  739.     print repr(p2.communicate()[0])
  740.     print 'Executing calc...'
  741.     p = Popen('calc')
  742.     p.wait()
  743.  
  744. if __name__ == '__main__':
  745.     if mswindows:
  746.         _demo_windows()
  747.     else:
  748.         _demo_posix()
  749.  
  750.